package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {
	@Autowired
   private IEmployeeService employeeService;

	@RequestMapping("")
	@PermissionName("员工列表")
	@RequiresPermissions("employee:index")
	public String index(){
		return "employee/list";
	}

    @RequestMapping("list")
	@ResponseBody
	@PermissionName("员工列表")
	@RequiresPermissions("employee:list")
	public PageResult list(EmployeeQueryObject qo){

        return employeeService.query(qo);
	}

	//判断用户是否存在
    @RequestMapping("validateUsername")
    @ResponseBody
    public boolean validateUsername(String username,Long id){
        Employee employee = employeeService.checkName(username);
        if (id==null){
            return employee==null;
        }else{
            return true;
        }
    }

	//保存
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("员工保存")
    @RequiresPermissions("employee:save")
    public JsonResult save(Employee entity){
        JsonResult result = new JsonResult();
        try{
            employeeService.insert(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("保存失败");
        }
        return  result;
    }
	//保存
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("员工更新")
    @RequiresPermissions("employee:update")
    public JsonResult update(Employee entity){
        JsonResult result = new JsonResult();
        try{
            employeeService.updateByPrimaryKey(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.markMsg("更新失败");
        }
        return  result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("employee:changeState")
    @PermissionName("员工状态")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            employeeService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("操作失败");
        }
        return result;
    }


}
